{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始DataFrame:\n",
      "       Name  Age\n",
      "10    Alice   25\n",
      "20      Bob   30\n",
      "30  Charlie   35\n",
      "40    David   40\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bww\\AppData\\Local\\Temp\\ipykernel_8268\\2877557028.py:1: DeprecationWarning: \n",
      "Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),\n",
      "(to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)\n",
      "but was not found to be installed on your system.\n",
      "If this would cause problems for you,\n",
      "please provide us feedback at https://github.com/pandas-dev/pandas/issues/54466\n",
      "        \n",
      "  import pandas as pd\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd  \n",
    "  \n",
    "# 创建一个示例DataFrame，其中索引不是连续的整数  \n",
    "data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],  \n",
    "        'Age': [25, 30, 35, 40]}  \n",
    "index = [10, 20, 30, 40]  # 非连续的索引  \n",
    "df = pd.DataFrame(data, index=index)  \n",
    "  \n",
    "print(\"原始DataFrame:\")  \n",
    "print(df)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "重置索引后的DataFrame:\n",
      "      Name  Age\n",
      "0    Alice   25\n",
      "1      Bob   30\n",
      "2  Charlie   35\n",
      "3    David   40\n"
     ]
    }
   ],
   "source": [
    "# 使用reset_index()重置索引  \n",
    "df_reset = df.reset_index(drop=True)  \n",
    "  \n",
    "print(\"\\n重置索引后的DataFrame:\")  \n",
    "print(df_reset)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "保留原始索引作为列后的DataFrame:\n",
      "   index     Name  Age\n",
      "0     10    Alice   25\n",
      "1     20      Bob   30\n",
      "2     30  Charlie   35\n",
      "3     40    David   40\n"
     ]
    }
   ],
   "source": [
    "# 如果我们想要保留原来的索引作为一个列，可以这样做：  \n",
    "df_reset_keep_index = df.reset_index(drop=False)  \n",
    "  \n",
    "print(\"\\n保留原始索引作为列后的DataFrame:\")  \n",
    "print(df_reset_keep_index)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pybroker",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
